<template>
    <component ref="current"
               v-if="currentView"
               :html="html"
               :is="currentView"
               :formAlias="formAlias"
               :single="single"
               :isTreeDialog="isTreeDialog"
               :formdata="formdata"
               :isDialog="isDialog"
               @confirm="confirm"
               @close="closeDialog"
               @onRefresh="onRefresh"
               :formJs="formJs"
               :appId="appId"
               :formBoListData="formBoListData"
    ></component>
    <RxLoading v-if="Loading"></RxLoading>
</template>

<script>
import {markRaw} from 'vue'
import FormBoListPreviewList from "./FormBoListPreviewList";
import FormBoListApi from "@/api/FormBoList/FormBoListApi";
import Util from "@/js/Util.js"
import BaseDialogComponent from "@/components/layer/BaseDialogComponent.js";
import RxLoading from '@/components/RxLoading'
export default {
    name: "FormBoListPreview",
    mixins: [BaseDialogComponent],
    components: {
        FormBoListPreviewList,
        RxLoading
    },
    props: {
        //对话框别名
        alias: {
            type: String
        },
        //数据权限别名
        pmtAlias: {
            type: String
        },
        //指定的应用
        appId: {
            type: String
        },
        //是否单选
        single: {
            type: Boolean
        },
        //参数例如 name=ray&address=guangzhou
        params: '',
        //表单关联数据 {name:"ray"}
        formdata: {
            type: Object,
            default: () => {
                return {}
            }
        },

    },
    data() {
        return {
            isPreview: true,
            currentView: '',
            html: "",
            isTreeDialog: "",
            isDialog: '',
            params_: "",
            aliasKey: this.alias,
            formAlias: '',
            formJs: {},
            Loading:true,
            formBoListData:{},//列表配置的全部属性
        }
    },
    mounted() {
        this.initParams();
    },
    methods: {
        closeDialog() {
            //this.dialogClose({action: "cancel"})
            this.$emit("close", {action: "cancel"});

        },
        confirm(data) {
            //this.dialogClose({action: "ok", data: data})
            this.$emit("confirm", Object.assign({action: 'ok'},data));
        },
        /**
         * 初始化参数
         * params 空串也算有参数，无则不传数据
         * 参数例如 name=ray&address=guangzhou
         */
        initParams(params) {
            //通过弹窗打开 Util.open
            if (this.alias) {
                this.params_ = params != undefined ? params : this.params;
                this.init();
            }
        },
        async init() {
            if (!this.aliasKey) {
                return;
            }
            let self = this;
            if(this.params_ && !this.params_.includes('single')){
                this.params_ = this.params_+'&single='+this.single
            }
            let parameter = {
                key: this.aliasKey,
                pmtAlias: this.pmtAlias,
                params: this.params_,
                appId: this.appId
            };
            let res = await FormBoListApi.getBolistByKey(parameter);
            if (!res.success) {
                return;
            }
            this.Loading = false;
            let record = res.data;
            self.formAlias = record.key;
            self.formBoListData = res.data;
            self.html = record.mobileHtml;
            self.isDialog = record.isDialog;
            self.isTreeDialog = record.isTreeDlg;
            self.currentView = markRaw(FormBoListPreviewList)
            let script = {};
            let scriptMath = this.html.match(/<script>([\s\S]*)<\/script>/);
            if (scriptMath != null) {
                script = scriptMath[1];
                script = Util.replaceDefault(script);
                script = new Function('return ' + script)();
            }
            self.formJs = script;
            //设置导航栏标题
            if(this.$route.path!='/views/modules/form/FormCustomView/FormCustomView'){
                uni.setNavigationBarTitle({
                    title: record.name
                });
            }
            this.$emit("init", record);

        },
        /**
         * 获取选中的数据.
         * @returns {*}
         */
        getSelected() {
            console.log('getSelectedgetSelectedgetSelected')
            return this.$refs.current.getData();
        },
        onRefresh() {
            this.$emit('onRefresh')
        },

    }
}
</script>
